package common;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.SocketTimeoutException;
import java.util.Map;

import mx.bigdata.jcalais.CalaisClient;
import mx.bigdata.jcalais.CalaisObject;
import mx.bigdata.jcalais.CalaisResponse;
import mx.bigdata.jcalais.rest.CalaisRestClient;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import common.cache.CacheManager;

import play.Play;
import play.cache.Cache;
import play.libs.WS;
import play.libs.WS.HttpResponse;
import play.libs.WS.WSRequest;

public class TagFetcher {

	public static Iterable<CalaisObject> getTags(String articleContent) {
		
		CalaisResponse response = null;
		CalaisClient client = new CalaisRestClient((String) CacheManager.getJavaCacheItem("calaisAPIKey"));
		
		try {
			response = client.analyze(articleContent);
		}
		
		catch(SocketTimeoutException e){
			System.err.println("Error In TagFeatcher With Socket TimeOutException");
			
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(NullPointerException e) {
			e.printStackTrace();
		}catch (IllegalArgumentException e) {
		   System.out.println("===========TagFeatcher Illegal Argument Error=======");
		}
        if(response  != null && response.getEntities().iterator().hasNext()){
		  return response.getEntities();
        }
        return null;
	}
}
